package com.sinosoft.cnmuseum.utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.commons.io.input.ReaderInputStream;
public class test2 {
		
		public static void main(String[] args) {
			new test2().getorg();
		}
		public final static String dbDriver = "com.mysql.jdbc.Driver";
		//public final static String dbDriver = "com.p6spy.engine.spy.P6SpyDriver";
		public final static String dbURL = "jdbc:mysql://localhost:3306/dpm_da?useUnicode=true&amp;characterEncoding=UTF-8";
		public final static String dbUser = "root";
		public final static String dbPass = "root";

		

		public void getorg(){
			try{
				Class.forName(dbDriver);
				Connection conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
				String sql ="INSERT INTO `dpm_da`.`archives_bt`(`name`, "
						+ "`parent_node`, `sort`, `type`) VALUES "
						+ "(?, ?, ?, ?)";

				String sql2 ="select * from `dpm_da`.`archives_bt` where name=?";
				String path = "C:\\Users\\wangli\\Desktop\\无标题.txt";
				PreparedStatement pst=conn.prepareStatement(sql);
				PreparedStatement pst2=conn.prepareStatement(sql2);
				
				FileInputStream is = new FileInputStream(new File(path));
				InputStreamReader isr = new InputStreamReader(is);
				BufferedReader br = new BufferedReader(isr);
				
				int i = 0;
				String linestr;
				String ppname = "";
				while ((linestr = br.readLine()) != null) {
					try {
						String ss[] = linestr.split("\t");
						if(ss.length==2){
							String pname = ss[0];
							String name = ss[1];
							if(ppname==""||!(ppname.equals(pname))){
								ppname = pname;
								i = 0;
							}
							if(ppname.equals(pname)){
								i = i+1;
							}
							pst2.setString(1, pname);
							ResultSet checkrs = pst2.executeQuery(); 
							int pid;
							if(checkrs.next())
								pid = checkrs.getInt("id");
							else
								continue;
							pst.setString(1, name);
							pst.setInt(2, pid);
							pst.setInt(3, i+1);
							pst.setString(4, "档案小类");
							pst.execute(); 
						}
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
				br.close();
				isr.close();
				is.close();
				pst.close();conn.close();
				System.out.println(1);
			}catch(Throwable tr){
				
			}
			
		}


		  private static final String CHARSET = "UTF-8";
		  private static final int DEFAULT_CONNECTION_TIMEOUT = (20 * 1000); // 20seconds
		  private static final int DEFAULT_SOCKET_TIMEOUT = (30 * 1000); // 30seconds
		
		public static String get(final String url) {
		    HttpURLConnection conn = null;
		    DataOutputStream outStream = null;
		    try {
		      String newUrl = url;
		      URL realUrl = new URL(newUrl);
		      conn = (HttpURLConnection) realUrl.openConnection();
		      conn.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
		      conn.setReadTimeout(DEFAULT_SOCKET_TIMEOUT);
		      conn.setUseCaches(false);
		      conn.setDoOutput(true);
		      conn.setRequestProperty("Connection", "Keep-Alive");
		      conn.setRequestProperty("Charset", CHARSET);
		      conn.setRequestProperty("user-agent",
		          "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
		      conn.connect();

		      if (conn.getResponseCode() == 200) {
		        InputStream in = conn.getInputStream();
		        StringBuffer sb = new StringBuffer();
		        InputStreamReader reader = new InputStreamReader(in, CHARSET);
		        char[] buff = new char[1024];
		        int len;
		        while ((len = reader.read(buff)) > 0) {
		          sb.append(buff, 0, len);
		        }
		        return sb.toString();
		      } else {
		        System.out.println(conn.getResponseCode());
		        throw new Exception("ResponseCode=" + conn.getResponseCode());
		      }

		    } catch (IOException ex) {

		    } catch (Exception e) {
		    } finally {
		      if (null != outStream) {
		        try {
		          outStream.close();
		        } catch (IOException e) {
		        }
		      }
		      if (null != conn) {
		        conn.disconnect();
		      }
		    }

		    return null;
		  }
	

}
